<?php
namespace app\index\controller;

use app\index\model\User;
use app\index\model\Profile;

class UserController{
	public function add(){
		/*$user = new UserModel;
		$user->nickname = '流月';
		$user->email = 'thinkphp@qq.com';
		$user->birthday = strtotime('1993-11-02');*/
		/*$user['nickname'] = '残叶2';
		$user['email'] = 'kancloud@qq.com';
		$user['birthday'] = '1994-02-25';//model get、set方法进行二次处理
		$result = User::create($user);
		*/
		$user = new User;
		
		$userAccount=array(
				'nickname' =>input('post.nickname'),
				'email'  => input('post.email'),
				'birthday' => input('post.birthday'),
				'token' => input('post.__token__'),
		);
		//$token = input('post.__token__');
		//$userAccount = input('post.');
		//dump($token);
		//exit();
		$result = $user->allowField(true)->validate(true)->save($userAccount);	
		
		//var_dump($result);
		
		if($result){
			return '新增成功!';
		}else{
			return $user->getError();
		}
	}
	
	public function addList(){
		$user = new User;
		$list = [
					['nickname' => '张三', 'email' => 'zhangsan@qq.com', 'birthday' => strtotime('1990-1-1')], 
					['nickname' => '李四', 'email' => 'lisi@163.com', 'birthday' => strtotime('1991-1-1')],
				];
		$result = $user->saveAll($list);
		if($result){
			return 'sccuess!';
		}else{
			return $user->getError();
		}
	}
	
	public function read($id=''){
		$user = User::get($id);
		//var_dump($user);
		//echo $user['nickname'].'<br/>';
		//echo $user['email'].'<br/>';
		//echo $user['birthday'].'<br/>';
		echo $user->name.'<br/>';
		echo $user->nickname.'<br/>';
		echo $user->profile->truename.'<br/>';
		echo $user->profile->email.'<br/>';
	}
	
	public function update($id=''){
		
		/*$user = array(
			'id' => (int)$id,
			'nname' => '张文',
			'email' => 'zhangwen@163.com',
		);
		$result = User::update($user);
		if($result){
			return 'update sccuess!';
		}else{
			return 'update fail!';
		}*/
		$user = User::get($id);
		$user->name = 'framework';
		if($user->save()){
			$user->profile->email = 'liu21st@gmail';
			$user->profile->save();
			return '用户['.$user->name.']';
			
		}else{
			return $user->getError();
		}
		
		
	}
	public function create(){
		return view();
	}
	
	public function addProfile(){
		$user = new User;
		
		$user->name = 'thinkphp';
		$user->password = '123456';
		$user->nickname = "流月";
		
		if($user->save()){
			$profile = new Profile;
			$profile->truename = '张文';
			$profile->birthday = '1977-10-05';
			$profile->address  = '上海';
 			$profile->email    = '123456@163.com';
 			$user->profile()->save($profile);
 			return "sccuess!";
			
		}else{
			return $user->getError();
		}	
			
	}
	
	public function readProfile($id=''){
		$user = User::get($id);
		

		echo $user->name.'<br/>';
		echo $user->nickname.'<br/>';
		echo $user->profile->truename.'<br/>';
		echo $user->profile->email.'<br/>';
		
	}
	
}

?>